package ua.kpi_java_training.zabrodskyi.module3;

import static org.junit.Assert.*;

import org.junit.Test;

/**
 * @author Maksym Zabrodskyi
 * @version 24-Jun-2013
 */
public class LifeTest {

	Life life;
	int lengthLife;

	@Test
	public void shouldCreateLife() {
		lengthLife = 5;
		life = new Life(lengthLife);
		assertNotNull(life);
	}

	@Test(expected = Exception.class)
	public void shouldExceptionBacauseLengthLifeIsZero() {
		lengthLife = 0;
		life = new Life(lengthLife);
	}

	@Test(expected = Exception.class)
	public void shouldExceptionBacauseLengthLifeIsNegative() {
		lengthLife = -4;
		life = new Life(lengthLife);
	}

	@Test
	public void shouldRunLifeAndBornNewVictim() {
		Environment environment = new Environment(Settings.getWidth(),
				Settings.getHeight());
		environment.add(ElementType.VICTIM);
		lengthLife = 14;
		life = new Life(lengthLife);
		life.run(environment);
		int actualCountVictims = environment
				.getCountElements(ElementType.VICTIM);
		assertEquals(2, actualCountVictims);
	}

	@Test
	public void shouldRunLifeAndPredatorsEatVictims() {
		Environment environment = new Environment(Settings.getWidth(),
				Settings.getHeight());
		environment.add(ElementType.PREDATOR);
		environment.add(ElementType.VICTIM);
		environment.add(ElementType.PREDATOR);
		environment.add(ElementType.VICTIM);
		Predator predator = (Predator) environment.getElement(0);
		Cell cell = new Cell(1, 1);
		predator.setCell(cell);
		Victim victim = (Victim) environment.getElement(1);
		cell = new Cell(2, 3);
		victim.setCell(cell);
		predator = (Predator) environment.getElement(2);
		cell = new Cell(2, 2);
		predator.setCell(cell);
		victim = (Victim) environment.getElement(3);
		cell = new Cell(3, 0);
		victim.setCell(cell);
		lengthLife = 2;
		life = new Life(lengthLife);
		life.run(environment);
		int actualCountElements = environment.getCountAllElements();
		assertEquals(2, actualCountElements);
	}

	@Test
	public void testLifeToString() {
		lengthLife = 4;
		life = new Life(lengthLife);
		String actualLifeToString = life.toString();
		assertEquals("Life goes 4 times", actualLifeToString);
	}

}
